package mobilesmil.userinterface;

import mobilesmil.activity.composer.ComposerActivity;
import android.content.Context;
import android.graphics.Rect;

/**
 * A rectangular construct that is used to position and resize a {@link SmilResource} on a {@link RegionPositioningView}.
 * Provides constraint verification preventing the user from stretching a resource beyond the canvas boundaries,
 * moving it beyond the canvas boundaries, or stretching into negative dimensions.
 * @author Ryan Ware
 *
 */
public class MoveableResourceRect {
	 private Rect mRect;
	 private Rect mResizeRect;
	 private static final byte RESIZER_RECT_WIDTH = 10;
	 private static final int CANVAS_HEIGHT = ComposerActivity.getEditingDataSet().getCanvasHeight();
	 private static final int CANVAS_WIDTH = ComposerActivity.getEditingDataSet().getCanvasWidth();

	 public MoveableResourceRect(Context context, int left, int top, int width, int height) {
		 mRect = new Rect(left, top, left + width, top + height);
		 recreateResizeRect();
	 }
	 
	/**
	 * Ensure the new top and left coordinates do not lie outside the canvas boundaries.
	 * If they do, they are re-valued to lie just inside the boundaries.
	 * @param newLeft new left coordinate
	 * @param newTop new top coordinate
	 */
	public void updateCoordinatesWithinCanvasBoundaries(int newLeft, int newTop) {
		if(mRect.right > CANVAS_WIDTH) // too far to the right
			newLeft = CANVAS_WIDTH - mRect.width();
		if(mRect.bottom > CANVAS_HEIGHT) // too low
			newTop = CANVAS_HEIGHT - mRect.height();
		
		if(newLeft < 0) // too far to the left
			newLeft = 0;
		if(newTop < 0) // too high
			newTop = 0;
		
		mRect = new Rect(newLeft, newTop, newLeft + mRect.width(), newTop + mRect.height());
		recreateResizeRect();
	}
	
	/**
	 * Ensure the new width and height values do not lie outside the canvas boundaries.
	 * If they do, they are re-valued to lie just inside the boundaries.
	 * @param newWidth new width
	 * @param newHeight new height
	 */
	public void updateDimensionsWithinCanvasBoundaries(int newWidth, int newHeight) {
		if(mRect.left + newWidth > CANVAS_WIDTH) // too wide
			newWidth = CANVAS_WIDTH - mRect.left;
		if(mRect.top + newHeight > CANVAS_HEIGHT) // too tall
			newHeight = CANVAS_HEIGHT - mRect.top;
		if(newHeight < 0) // negative height
			newHeight = 0;
		if(newWidth < 0) // negative width
			newWidth = 0;
		
		mRect = new Rect(mRect.left, mRect.top, mRect.left + newWidth, mRect.top + newHeight);
		recreateResizeRect();
	}
	
	/**
	 * Redraws the small resizing {@link Rect} so it always sits on the bottom right hand corner
	 * of this {@link MoveableResourceRect}.
	 */
	private void recreateResizeRect() {
		mResizeRect = new Rect(mRect.right - RESIZER_RECT_WIDTH, 
 				mRect.bottom - RESIZER_RECT_WIDTH,
 				mRect.right + RESIZER_RECT_WIDTH,
 				mRect.bottom + RESIZER_RECT_WIDTH);
	}
	
	public Rect getRect() {
		return mRect;
	}

	public void setRect(Rect rect) {
		this.mRect = rect;
	}

	public Rect getResizeRect() {
		return mResizeRect;
	}
	
}
